/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.server.util;

import java.security.InvalidKeyException;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.niiuzu.server.util.exception.IllegalCryptedKeyValueException;
import com.niiuzu.server.util.exception.IllegalCryptedTextValueException;
import com.niiuzu.server.util.exception.WrongBytesStringFormatException;

/**
 * Classe utilitaire contenant les méthodes dédiées à la cryptographie
 * symétrique.
 * 
 * @see <a
 *      href="http://en.wikipedia.org/wiki/Symmetric_key_algorithm">http://en.wikipedia.org/wiki/Symmetric_key_algorithm</a>
 */
public final class SymCryptUtil {

	private static final String CRYP_ALGO = "Blowfish";

	private static final String CHARSET_NAME_UTF8 = "UTF-8";

	/**
	 * Construit une nouvelle instance de {@link SymCryptUtil}.
	 */
	private SymCryptUtil() {
	}

	/**
	 * Crypte un texte à partir d'une clef.
	 * 
	 * @param key
	 *            valeur de la clef de cryptage.
	 * @param plainText
	 *            valeur du texte à crypter.
	 * 
	 * @return résultat du cryptage sous forme de {@link String}.
	 * 
	 * @throws IllegalCryptedKeyValueException
	 *             mauvaise valeur de la clef de cryptage.
	 */
	public static String encrypt(String key, String plainText)
			throws IllegalCryptedKeyValueException {
		if (plainText == null) {
			throw new IllegalArgumentException("Plain text cannot be null.");
		}

		String cryptedText = null;

		byte[] keyBytes;
		try {
			keyBytes = BytesUtil.convertStringToBytes(key);
		} catch (WrongBytesStringFormatException e) {
			throw new IllegalCryptedKeyValueException(e);
		}

		try {

			Cipher cipher = Cipher.getInstance(CRYP_ALGO);
			Key secretKey = new SecretKeySpec(keyBytes, CRYP_ALGO);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);

			byte[] plainTextBytes = plainText.getBytes(CHARSET_NAME_UTF8);
			byte[] cryptedTextBytes = cipher.doFinal(plainTextBytes);

			cryptedText = BytesUtil.convertBytesToString(cryptedTextBytes);

		} catch (InvalidKeyException e) {
			throw new IllegalCryptedKeyValueException(e);
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}

		return cryptedText;
	}

	/**
	 * Décrypte un texte à partir d'une clef et du résultat du cryptage.
	 * 
	 * @param key
	 *            valeur de la clef de cryptage.
	 * @param cryptedText
	 *            valeur cryptée du texte.
	 * 
	 * @return valeur d'origine du texte.
	 * 
	 * @throws IllegalCryptedKeyValueException
	 *             mauvaise valeur de la clef de cryptage.
	 * @throws IllegalCryptedTextValueException
	 *             mauvaise valeur du texte crypté.
	 */
	public static String decrypt(String key, String cryptedText)
			throws IllegalCryptedKeyValueException,
			IllegalCryptedTextValueException {

		byte[] keyBytes = null;
		try {
			keyBytes = BytesUtil.convertStringToBytes(key);
		} catch (WrongBytesStringFormatException e) {
			throw new IllegalCryptedKeyValueException(e);
		}

		byte[] cryptedTextBytes = null;
		try {
			cryptedTextBytes = BytesUtil.convertStringToBytes(cryptedText);
		} catch (WrongBytesStringFormatException e) {
			throw new IllegalCryptedTextValueException(e);
		}

		String plainText = null;

		try {

			Cipher cipher = Cipher.getInstance(CRYP_ALGO);
			Key secretKey = new SecretKeySpec(keyBytes, CRYP_ALGO);
			cipher.init(Cipher.DECRYPT_MODE, secretKey);

			byte[] plainTextBytes = cipher.doFinal(cryptedTextBytes);
			plainText = new String(plainTextBytes, CHARSET_NAME_UTF8);

		} catch (InvalidKeyException e) {
			throw new IllegalCryptedKeyValueException(e);
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}

		return plainText;
	}
}
